<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>组织编辑</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/style/custom.css}" media="all">
</head>
<body>

<form class="layui-form" action="" lay-filter="organization-form-edit" id="organization-form-edit" style="padding: 20px 30px 0 0;">
  <input type="hidden" id="id" name="id" th:value="${organization.id}"/>
  <input type="hidden" name="orgCode" th:value="${organization.orgId}"/>
  <div class="layui-form-item">
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <label class="layui-form-label">组织名称</label>
      <div class="layui-input-block">
        <input type="text" name="orgName" th:value="${organization.orgName}" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
      </div>
    </div>
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <label class="layui-form-label">父级组织</label>
      <div class="layui-input-block " id="parentId">
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <label class="layui-form-label">组织类型</label>
      <div class="layui-input-block">
        <select name="orgType" lay-filter="orgType-select" lay-verify="required">
          <option value="">请选择</option>
          <option th:each="map,idx:${orgTypes}" th:value="${map.key}" th:selected="${map.key == organization.orgType}" th:text="${map.value}"></option>
        </select>
      </div>
    </div>
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <label class="layui-form-label">组织层级</label>
      <div class="layui-input-block">
        <select name="orgLevel" lay-filter="orgLevel-select" lay-verify="required">
          <option value="">请选择</option>
          <option th:each="map,idx:${orgLevels}" th:value="${map.key}" th:selected="${map.key eq organization.orgLevel}" th:text="${map.value}"></option>
        </select>
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <label class="layui-form-label">办公电话</label>
      <div class="layui-input-block">
        <input type="text" name="phone" th:value="${organization.phone}" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <label class="layui-form-label">状态</label>
      <div class="layui-input-block">
        <input type="radio" name="status" value="Y" title="启用" th:checked="${ organization.status == null || organization.status == 'Y'} ? true">
        <input type="radio" name="status" value="N" title="禁用" th:checked="${ organization.status == 'N'} ? true">
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <label class="layui-form-label">地址</label>
      <div class="layui-input-block">
        <textarea name="orgAddress" placeholder="请输入" class="layui-textarea" th:text="${organization.orgAddress}"></textarea>
      </div>
    </div>
  </div>
  <div class="layui-form-item layui-hide">
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
      <div class="layui-input-block" style="text-align:center;">
        <input type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="LAY-organization-front-submit" id="LAY-organization-front-submit" value="提交">
        <input type="button" class="layui-btn layui-btn-primary" lay-filter="LAY-organization-front-reset" id="LAY-organization-front-reset" value="重置">
      </div>
    </div>
  </div>
</form>

<script th:inline="javascript">
  let parentId = [[${organization.parentId}]];
</script>
<script th:src="@{/layui/layui.js}"></script>
<script>
  layui.config({
    base: '/'
  }).extend({
    xmSelect: 'extend/xm-select/dist/xm-select'
  }).use(['index', 'xmSelect'], function () {
    let xmSelect = layui.xmSelect;
    let admin = layui.admin;
    let form = layui.form;

    //父级组织
    let parentId_select = xmSelect.render({
      el: '#parentId',
      name: 'parentId',
      layVerify: 'required',
      layVerType: 'msg',
      //显示为text模式
      model: {label: {type: 'text'}},
      //单选模式
      radio: true,
      //选中关闭
      clickClose: true,
      toolbar: {
        show: true,
        list: ['CLEAR']
      },
      filterable: true,
      //树
      tree: {
        show: true,
        //非严格模式
        strict: false,
        //展开所有节点
        expandedKeys: true,
      },
      data: []
    });

    admin.req({
      url: '/sys/organization/getSelectList',
      type: 'post',
      done: function (res) {
        parentId_select.update({
          data: res.data,
          autoRow: true,
        });
        //异步远程加载数据，需要重新设置
        parentId_select.changeExpandedKeys(true);
        if (parentId) {
          parentId_select.setValue([parentId]);
        }
      }
    });

    //提交
    form.on('submit(LAY-organization-front-submit)', function (data) {
      let inx = layer.load(0, {shade: false});
      let field = data.field;

      admin.req({
        url: '/sys/organization/save',
        type: 'post',
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(field),
        done: function (res) {
          layer.close(inx);
          //执行成功
          let index = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
          layer.msg(res.msg, {icon: 1, time: 1600}, function () {
            parent.layer.close(index);//关闭弹出层
          });
        }
      });
      return false; // 阻止默认 form 跳转
    });

    //重置表单 包含有下拉tree需要特殊处理
    layui.$("#LAY-organization-front-reset").click(function () {
      layui.$("#organization-form-edit")[0].reset();
      parentId_select.setValue(parentId ? [parentId] : []);
      form.render();
    });
  });
</script>
</body>
</html>